package com.lmc.adapter;

import android.content.Context;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lmc.R;

import java.util.List;

public class FeedbackImgAdapter extends RecyclerView.Adapter<FeedbackImgAdapter.ViewHolder> {
    Context context;
    List<String> img_list;
    OnClick onClick;

    public FeedbackImgAdapter(Context context, List<String> img_list, OnClick onClick) {
        this.context = context;
        this.onClick = onClick;
        this.img_list = img_list;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FeedbackImgAdapter.ViewHolder(LayoutInflater.from(context).inflate(R.layout.lmc_feedback_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        final String s = img_list.get(position);
        if (TextUtils.isEmpty(s)) {
            holder.img.setImageResource(R.drawable.lmc_tianjia);
            holder.del.setVisibility(View.GONE);
        } else {
            holder.del.setVisibility(View.VISIBLE);
            holder.img.setImageURI(Uri.parse(s));
        }

        holder.img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(s)) {
                    //添加图片
                    onClick.add();
                }
            }
        });
        holder.del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onClick.del(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return img_list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView img, del;

        public ViewHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.img);
            del = itemView.findViewById(R.id.del);
        }
    }

    public interface OnClick {
        void add();

        void del(int i);
    }
}
